package com.polytech.tablespaceResolver.configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import com.polytech.tablespaceResolver.ORMUtils.DBCredentials;

public class PropertyLoader {
	private static String PROPERTY_FILENAME = "resolver.xml";
	private static PropertyLoader loader = null;
	
	private PropertyLoader() {}
	
	public static PropertyLoader getInstance()
	{
		if(loader == null)
			loader = new PropertyLoader();
		return loader;
	}
	public DBCredentials loadCredentials()
	{
		Properties properties = new Properties();
        FileInputStream iFile;
		try {			
			iFile = new FileInputStream(PROPERTY_FILENAME);			
			properties.loadFromXML(iFile);
		
			return new DBCredentials.Builder()
												.setDbName(new String(properties.getProperty("DB_NAME")))
												.setHost(new String(properties.getProperty("HOST")))
												.setLogin(new String(properties.getProperty("LOGIN")))
												.setPassword(new String(properties.getProperty("PASSWORD")))
												.setPort(new String(properties.getProperty("PORT")))
												.setSchema(new String(properties.getProperty("SCHEMA")))
											.build();
            
		} catch (FileNotFoundException e) {
			System.err.println("FileNotFoundException while reading the configuration file in PropertyReader.");
			e.printStackTrace();
		} catch (InvalidPropertiesFormatException e) {
			System.err.println("Invalid property exception while reading the configuration file in PropertyReader.");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("IO exception while reading the configuration file in PropertyReader.");
			e.printStackTrace();
		}
		return null;
	}
}
